package com.example.demo.controller;

import com.example.demo.entity.Integral;
import com.example.demo.entity.blog;
import com.example.demo.entity.users;
import com.example.demo.mapper.IntegralMapper;
import com.example.demo.mapper.blogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
public class integralController {
    @Autowired
    IntegralMapper integralMapper;
    @Autowired
    blogMapper blogMapper;

    //积分修改他人操作
    @GetMapping("/updateIntegral/{id}/{mailbox}/{integral}")
    public String updateIntegral(@PathVariable int id, @PathVariable String mailbox, @PathVariable int integral) {
        Integral i = new Integral();
        i.setIntegral(integral + integralMapper.selectinterfaceBymailbox(mailbox).getIntegral());
        i.setMailbox(mailbox);
        blog blog = blogMapper.selectblog(id);
        if (!mailbox.equals(blog.getUser())) {
            integralMapper.updateIntegral(i);
            i.setMailbox(blog.getUser());
            if (integral == 5) {
                i.setIntegral(5+integralMapper.selectinterfaceBymailbox(blog.getUser()).getIntegral());
            } else if (integral == 2) {
                i.setIntegral(3 +integralMapper.selectinterfaceBymailbox(blog.getUser()).getIntegral());
            } else if (integral == 1) {
                i.setIntegral(2+integralMapper.selectinterfaceBymailbox(blog.getUser()).getIntegral());
            } else if (integral == 3) {
                i.setIntegral(8+integralMapper.selectinterfaceBymailbox(blog.getUser()).getIntegral());
            }
            integralMapper.updateIntegral(i);
        }

        return i.toString();
    }
    //积分修改自己操作(废弃方法)
    @GetMapping("/updateIntegralziji/{id}/{mailbox}/{integral}")
    public String updateIntegralziji(@PathVariable int id, @PathVariable String mailbox, @PathVariable int integral){
        Integral i = new Integral();
        i.setIntegral(integral + integralMapper.selectinterfaceBymailbox(mailbox).getIntegral());
        i.setMailbox(mailbox);
        blog blog = blogMapper.selectblog(id);
        if (!mailbox.equals(blog.getUser())) {
            integralMapper.updateIntegral(i);
            i.setMailbox(blog.getUser());
            if (integral == 5) {
                i.setIntegral(5+integralMapper.selectinterfaceBymailbox(blog.getUser()).getIntegral());
            } else if (integral == 3) {
                i.setIntegral(2 +integralMapper.selectinterfaceBymailbox(blog.getUser()).getIntegral());
            } else if (integral == 2) {
                i.setIntegral(1+integralMapper.selectinterfaceBymailbox(blog.getUser()).getIntegral());
            } else if (integral == 8) {
                i.setIntegral(3+integralMapper.selectinterfaceBymailbox(blog.getUser()).getIntegral());
            }
            integralMapper.updateIntegral(i);
        }

        return i.toString();
    }
}
